import Vue from 'vue'
import router from './router'
import store from './store/index';


//缓存micro
router.beforeEach( async (to, from, next) =>{
    console.log("microConfigList",store.state.microConfigList);
    console.log("to.path",to.path);
    const microConf = store.state.microConfigList.find(item => to.path.indexOf(item.activeRule) !== -1)
    if(microConf){
        // 未切换子应用
        if(store.state.currentMicro &&  store.state.currentMicro.activeRule === microConf.activeRule ){
            console.log("store.state.sessionUser",store.state.sessionUser)
            actions.setGlobalState({sessionUser: store.state.sessionUser});//给微前端传session
            console.log("未切换子应用")
            next()
            return
        }
        const cacheMicro = store.state.microList.find(item => microConf.activeRule===item.activeRule)
        // 已缓存应用
        if(cacheMicro){
            console.log("已缓存应用")
            next()
            return
        }
        // 未缓存应用
        const micro = loadMicroApp({...microConf, router})
        store.commit('addMicro',{activeRule:microConf.activeRule,micro:micro});
        store.commit('setCurrentMicro',microConf);
        actions.setGlobalState({sessionUser: store.state.sessionUser});//给微前端传session
        //currentMicro = microConf
        console.log("未缓存应用")
        next()
    }
    console.log("主应用内跳转")
    actions.setGlobalState({sessionUser: store.state.sessionUser});//给微前端传session
    //主应用内跳转
    next()
})

const request = url =>
fetch(url, {
    referrerPolicy: 'origin-when-cross-origin',
});

//设置哪个子应用程序在主加载后默认处于活动状态
//setDefaultMountApp('/home');

//第一个应用构建完成后执行要执行的函数
runAfterFirstMounted(() => console.info('first app mounted'));
start({ prefetch: true, fetch: request });
import { loadMicroApp,runAfterFirstMounted, start, initGlobalState,MicroAppStateActions} from 'qiankun';
const initialState = {}
let  actions = initGlobalState(initialState)
Vue.prototype.$actions = actions
Vue.prototype.$loadMicroApp = loadMicroApp






